package com.openbravo.pos.panels;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JComboBox;
import javax.swing.JButton;

import e.odbo.data.bean.I_Category;

import com.openbravo.data.gui.ComboBoxValModel;
import com.openbravo.pos.panels.event.AbstractCategoryChangedListener;

import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;

public class ComboBoxCategoryFilter<T extends I_Category> extends JPanel implements ItemListener {
	private ComboBoxValModel m_CategoryModel;
	/**
	 * Create the panel.
	 */
	private JComboBox m_jCategory;
	public ComboBoxCategoryFilter() {
		initComponents();
		m_CategoryModel=new ComboBoxValModel();
		m_jCategory.setModel(m_CategoryModel);
		m_jCategory.addItemListener(this);
	}
	
	private void initComponents() {
		setLayout(new BorderLayout(0, 0));
		
		m_jCategory = new JComboBox();
		add(m_jCategory, BorderLayout.CENTER);
		
		JButton jSearchCategory = new JButton();
		jSearchCategory.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/search.png"))); // NOI18N
        jSearchCategory.setFocusable(false);
        jSearchCategory.setPreferredSize(new java.awt.Dimension(35, 25));
        jSearchCategory.setRequestFocusEnabled(false);
        jSearchCategory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jSearchCategoryActionPerformed(evt);
            }
        });
		add(jSearchCategory, BorderLayout.EAST);
	}
	protected void jSearchCategoryActionPerformed(ActionEvent evt) {
		JCategoryTreeDialog dailog=JCategoryTreeDialog.newJDialog(this);
        dailog.showDialog(m_CategoryModel.getDatas(),(I_Category) m_CategoryModel.getSelectedItem());
        if(dailog.isOK())
            m_CategoryModel.setSelectedItem(dailog.getSelected());
	}
	
	public void addAbstractCategoryChangedListener(AbstractCategoryChangedListener l){
		listenerList.add(AbstractCategoryChangedListener.class, l);
	}
	
	public void removeAbstractCategoryChangedListener(AbstractCategoryChangedListener l){
		listenerList.remove(AbstractCategoryChangedListener.class, l);
	}
	
	public void loadData(List<I_Category> datas){
		m_CategoryModel.refresh(datas);
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		m_CategoryModel.getSelectedItem();
		for(AbstractCategoryChangedListener l:listenerList.getListeners(AbstractCategoryChangedListener.class))
			l.fireCategoryChanged((I_Category) m_CategoryModel.getSelectedItem());
		
	}

}
